require([
  "jquery",
  "ko",
  "bootstrap",
  "jquery-select2",
  "jquery-gritter",
  "jquery-blockui",
  "jquery-slimscroll",
  "akcy"
], function ($, ko) {
  var akcy = arguments[arguments.length - 1];
  akcy.ui.init();

  akcy.ajaxget("monitor", 'config/SysDicInfoList').done(function (dic) {

    var tankArea = akcy.tree().options({
      dataQueryUrl: 'config/TankAreaList'
    }).optionsExtend({
      appId: "monitor",
      searched: function (data) {


      },
      nodeClicked: function (data, e) {
        table.getInstance().tankAreaId(data.tankAreaId);
        sampling.getInstance().tankCode(null);
        table.getInstance().tankAreaName(data.tankAreaAlias);
        table.load();
        e.stopPropagation();
      }
    }).bind("tankArea").load();

    var sampling = akcy.table().options({
        dataQueryUrl: 'config/TankAreaList',
        headers: [

          {
            text: '采样点名称'
          }, {
            text: '采样点编码'
          }, {
            text: '样品名称'
          }, {
            text: '样品编码'
          }
        ],

      },

    ).optionsExtend({
      appId: "monitor",
      isCheckedAll: ko.observable(false),
      searched: function (data) {


      },
      tankCode: ko.observable(null),
      addMod: function () {

        if (sampling.getInstance().tankCode() == null) {

          akcy.alert.info('请选择罐！');
          return;
        }

        sampling.getInstance().recordSet.push({
          id: '',
          samplingName: '',
          samplingCode: '',
          samplingPointName: '',
          samplingPointCode: '',
          parentTankCode: sampling.getInstance().tankCode(),

        });
      },
      search: function () {

        akcy.ajaxget('monitor', 'config/SamplingPointInfoList/' + sampling.getInstance().tankCode()).done(function (data) {

          sampling.getInstance().recordSet(data);


        })


      },
      save: function () {

        akcy.ajaxpost('monitor', 'config/saveSamplingPointInfo', {
          samplingPointList: sampling.getInstance().recordSet()
        }).done(function () {
          akcy.alert.success('保存成功！');
          sampling.getInstance().search();


        })
      },
      delete: function (data) {

        akcy.confirm('确认删除吗', function () {
          if (data.id) {
            akcy.ajaxdelete('monitor', 'config/deleteSamplingPoint/' + data.id).done(function (data) {
              sampling.getInstance().search();
              akcy.alert.success('删除成功！');
            })
          } else {
            sampling.getInstance().recordSet.remove(data);
            akcy.alert.success('删除成功！');

          }
        })
      },
      nodeClicked: function (data, e) {
        table.getInstance().tankAreaId(data.tankAreaId);

        table.getInstance().tankAreaName(data.tankAreaAlias);
        table.load();
        e.stopPropagation();
      }
    }).bind("sampling");
    var table = akcy.table().options({
      fields: [{
        field: 'tankAreaId',
        value: ko.observable(0)
      }],
      headers: [{
        text: '罐名称',

        width: '10%'
      }, {
        text: '罐类型',

        width: '11%'
      }, {
        text: '罐编码',

        width: '5%'
      }, {
        text: '罐区',

        width: '11%'
      }],
      // defaultOrderBy: "U_ID",
      dataQueryUrl: 'config/TankInfoList'
    }).optionsExtend({
      appId: "monitor",
      tankAreaName: ko.observable(),
      branches: ko.observableArray([]),
      nodes: ko.observableArray([]),
      searched: function (data) {},
      adduser: function () {


      },
      edituser: function (data) {
        actionTable.getInstance().etlid(pageForm.getInstance().id());
        actionTable.load();
      },
      isCheckedAll: ko.observable(false),
      checkedAll: function () {
        var array = [];
        if (table.getInstance().isCheckedAll() == false) {
          table.getInstance().isCheckedAll(true);
          $(table.getInstance().recordSet()).each(function () {
            array.push($(this)[0].tankId);
          });
        } else {
          table.getInstance().isCheckedAll(false);
        }
        table.getInstance().checkedUser(array);
      },
      checkedUser: ko.observableArray(),
      checkService: function (data) {
        if (!table.getInstance().checkedUser().contains(data.id))
          table.getInstance().checkedUser().insert(0, data.id);
      },
      rowclick: function (data) {
        sampling.getInstance().tankCode(data.tankCode);
        sampling.getInstance().search();


      },
      exece: function (data) {
        prameTable.getInstance().etlid(data.id);
        prameTable.getInstance().serviceData(data);
        prameTable.load();
        akcy.showmodal("modal-user");
      },
      addService: function () {

      },
      deleteService: function () {
        akcy.confirm('确认删除吗？', function () {
          akcy.ajaxdelete('config/DeleteTank/' + table.getInstance().checkedUser().join(','), {}).done(function () {
            akcy.alert.success("删除成功！");
            table.load();
            prameTable.getInstance().etlid(0);
            prameTable.load();
          })
        })
      }
    }).bind("tankList").load();
  })

});